package com.an.pay.aspect.wx.pc;

import com.an.pay.config.Decrypt;
import com.an.pay.config.WxPayConfig;
import com.an.pay.util.HttpUtils;
import com.an.pay.util.WechatPay2ValidatorForRequest;
import com.google.gson.Gson;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@Aspect
public class WxRefundNotifyAspect {

    @Resource
    private Verifier verifier;
    @Resource
    private WxPayConfig wxPayConfig;
    @Autowired
    private Decrypt decrypt;


    @Pointcut(value = "@annotation(com.an.pay.annotation.WxRefundNotify)")
    public void point(){

    }

    @Around(value = "point()")
    public String around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取HttpServletRequest，HttpServletResponse对象
        HttpServletRequest request = null;
        HttpServletResponse response = null;

        for (Object arg : joinPoint.getArgs()) {
            if(arg instanceof ServletRequest){
                request = (HttpServletRequest) arg;
            }else if(arg instanceof ServletResponse) {
                response = (HttpServletResponse) arg;
            }
        }

        log.info("退款通知执行");
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();//应答对象

        try {
            //处理通知参数
            String body = HttpUtils.readData(request);
            Map<String, Object> bodyMap = gson.fromJson(body, HashMap.class);
            String requestId = (String)bodyMap.get("id");
            log.info("支付通知的id ===> {}", requestId);

            //签名的验证
            WechatPay2ValidatorForRequest wechatPay2ValidatorForRequest
                    = new WechatPay2ValidatorForRequest(verifier, requestId, body);
            if(!wechatPay2ValidatorForRequest.validate(request)){

                log.error("通知验签失败");
                //失败应答
                response.setStatus(500);
                map.put("code", "ERROR");
                map.put("message", "通知验签失败");
                return gson.toJson(map);
            }
            log.info("通知验签成功");

            //把密文 解密为 明文
            String plainText = decrypt.decryptFromResource(bodyMap);
            log.info("微信请求传回的明文：{}",plainText);

            //处理退款单
//            wxPayService.processRefund(bodyMap);
            joinPoint.proceed(new Object[]{request,response,plainText});

            //成功应答
            response.setStatus(200);
            map.put("code", "SUCCESS");
            map.put("message", "成功");
            return gson.toJson(map);

        } catch (Exception e) {
            e.printStackTrace();
            //失败应答
            response.setStatus(500);
            map.put("code", "ERROR");
            map.put("message", "失败");
            return gson.toJson(map);
        }

    }
}
